package com.yoomiito.app.adapter.cicle;

import android.net.Uri;
import androidx.annotation.Nullable;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yoomiito.app.Constant;
import com.yoomiito.app.R;
import com.yoomiito.app.base.App;
import com.yoomiito.app.model.cicle.CicleRecommendInfo;
import com.yoomiito.app.model.cicle.ImageItemBean;
import com.yoomiito.app.utils.AllUtils;
import com.yoomiito.app.utils.ResUtil;
import com.yoomiito.app.utils.YMToast;
import com.yoomiito.app.widget.CircleImageView;
import com.yoomiito.app.widget.SpacesItemDecoration;

import java.util.ArrayList;
import java.util.List;

public class CicleGoodRemmendAdapter extends BaseQuickAdapter<CicleRecommendInfo.DataBean, BaseViewHolder> {
    public CicleGoodRemmendAdapter(@Nullable List<CicleRecommendInfo.DataBean> data) {
        super(R.layout.item_cicle, data);
    }

    @Override
    protected void convert(final BaseViewHolder helper, CicleRecommendInfo.DataBean item) {
        helper.setGone(R.id.layout_good,false);
        helper.addOnClickListener(R.id.tv_share);
        RequestOptions requestOptions = new RequestOptions();
        requestOptions.placeholder(R.drawable.goods_default);
        if (!TextUtils.isEmpty(item.getAvatar())) {
            Glide.with(App.getApplication()).load(Uri.parse(item.getAvatar())).apply(requestOptions).into((CircleImageView) helper.getView(R.id.iv_photo));
        }

        ((TextView) helper.getView(R.id.tv_name)).setText(item.getAuthor());
        ((TextView) helper.getView(R.id.tv_date)).setText("" + item.getCtime());
        TextView contentTv = helper.getView(R.id.tv_content);
        contentTv.setText("" + item.getContent());
        contentTv.setOnClickListener(v -> {
            AllUtils.copyText(contentTv.getContext(),item.getContent());
            YMToast.showText("复制成功");
        });

        List<ImageItemBean> imageBeanList = new ArrayList<>();
        if (item.getContent_img() != null && item.getContent_img().size() > 0) {
            for (String url : item.getContent_img()) {
                imageBeanList.add(new ImageItemBean(url,item.getPrice()));
            }
            ImageAdapter adapter = new ImageAdapter(imageBeanList);
            adapter.setOnItemChildClickListener((adapter1, view, position) -> {
                getOnItemChildClickListener().onItemChildClick(adapter1,view,helper.getLayoutPosition());
            });
            RecyclerView recyclerView = (RecyclerView) helper.getView(R.id.rv_photo);
            recyclerView.setLayoutManager(new GridLayoutManager(mContext, Constant.GRID_IMAGE_NUM));
            if (recyclerView.getItemDecorationCount() >0){//避免重复添加itemDecoration
                RecyclerView.ItemDecoration itemDecoration = recyclerView.getItemDecorationAt(0);
                if(itemDecoration == null){
                    recyclerView.addItemDecoration(new SpacesItemDecoration(ResUtil.getSize(R.dimen.dimen_6dp)));
                }
            }else{
                recyclerView.addItemDecoration(new SpacesItemDecoration(ResUtil.getSize(R.dimen.dimen_6dp)));
            }

            recyclerView.setAdapter(adapter);
        }
    }


}
